<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Role;
use Illuminate\Http\Request;

class RoleController extends Controller {


    //列表
    public function index(Request $request){
        $params = [
            'kwd'    => (string)$request->get('kwd',''),
            'status' => (int)$request->get('status',0),
        ];
        $lists = Role::getList($params);
        return $this->writeJson(0,$lists);
    }

    //所有角色
    public function all(){
        $roles = Role::roles();
        return $this->writeJson(0,$roles);
    }

    //更新
    public function update(Request $request)
    {
        $data = $request->all();
        if($data['id']){
            $info = Role::findOrFail($data['id']);
            $info->update($data);
            return $this->writeJson(0,null,'修改角色信息成功');
        }else{
            unset($data['id']);
            $data['access_ids'] = [];
            Role::create($data);
            return $this->writeJson(0,null,'新增角色信息成功');
        }
    }

    //锁定
    public function lock(Request $request){
        $id = (int)$request->get('id',0);
        if(empty($id)){
            return $this->writeJson(1,null,'参数错误');
        }
        $info = Role::findOrFail($id);
        $info->update(['status'=>Role::STATUS_2]);
        return $this->writeJson(0);
    }

    //激活
    public function active(Request $request){
        $id = (int)$request->get('id',0);
        if(empty($id)){
            return $this->writeJson(1,null,'参数错误');
        }
        $info = Role::findOrFail($id);
        $info->update(['status'=>Role::STATUS_1]);
        return $this->writeJson(0);
    }

    //删除
    public function destroy(Request $request){
        $id = (int)$request->get('id',0);
        if(empty($id)){
            return $this->writeJson(1,null,'参数错误');
        }
        Role::destroy($id);
        return $this->writeJson(0);
    }

}
